﻿using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using ProjBackEnd.Areas.Sys.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Sys.Controllers
{
    /// <summary>
    /// 菜单操作
    /// </summary>
    public class MenuOptController : ApiController
    {
        /// <summary>
        /// 获取菜单信息，用于展示菜单所挂载的功能
        /// </summary>
        /// <param name="key">memberid|登录平台</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/sys/menuopt")]
        public RespSysMenuInfo GetMenuInfo(string key)
        {
            RespSysMenuInfo resp = new RespSysMenuInfo();

            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length != 2)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strPlatcd = strsKeyOriginal[1];

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            if (!basecodeLogic.CheckUserPermission(strMemberid, GetType().FullName))
            {
                resp.statusCode = 90015;
                resp.statusMsg = " 没有相关的操作权限";
                return resp;
            }

            resp = basecodeLogic.GetMenuFeatureAll();
            return resp;
        }

        /// <summary>
        /// 修改菜单基础信息
        /// </summary>
        /// <param name="_reqInfo">菜单操作请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/sys/menuopt")]
        public RespSysNullResultInfo UpdateMenuInfo(ReqMenuManagement _reqInfo)
        {
            RespSysNullResultInfo resp = new RespSysNullResultInfo();

            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            if (!basecodeLogic.CheckUserPermission(_reqInfo.memberid, GetType().FullName))
            {
                resp.statusCode = 90015;
                resp.statusMsg = " 没有相关的操作权限";
                return resp;
            }

            bool isUpdSuccess = false;
            foreach (MenuAll _entity in _reqInfo.menulist)
            {
                isUpdSuccess = basecodeLogic.UpdateMenuInfo(_reqInfo.memberid, _entity);
            }

            if (isUpdSuccess)
            {
                resp.statusCode = 0;
                resp.statusMsg = "更新成功";
            }
            else
            {
                resp.statusCode = 60007;
                resp.statusMsg = "功能菜单信息更新失败";
            }

            return resp;
        }
    }
}
